package com.zxc.controller.chart;

import com.zxc.entity.CstCustomer;
import com.zxc.entity.Orders;
import com.zxc.entity.OrdersLine;
import com.zxc.service.CstCustomerService;
import com.zxc.service.OrdersLineService;
import com.zxc.service.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/chart/contribution")
public class CustomerContributionController {

    @Autowired
    private CstCustomerService cstCustomerService;

    @Autowired
    private OrdersService ordersService;

    @Autowired
    private OrdersLineService ordersLineService;

    @RequestMapping({"/init/{custName}/{searchYear}","/init","/init/{custName}","/init/{searchYear}"})
    @ResponseBody
    public List<Map<String,Object>> getCustomerContribution(@PathVariable(required = false) String custName,
                                                            @PathVariable(required = false) String searchYear){
        List<Map<String,Object>> mapList = new ArrayList<>();

        //需要客户表的客户数据的编号和客户名称
        List<CstCustomer> cstCustomerList = cstCustomerService.selectAllCustomer();

        //根据客户名称进行模糊查询 TODO
        if (custName != null && !custName.equals("")){
            cstCustomerList = cstCustomerService.selectCstCustomerByCustNameForChart(custName);
        }

        for (CstCustomer cstCustomer : cstCustomerList){

            //一个List代表一条数据
            Map<String,Object> modelMap  = new HashMap<>();
            //存入客户编号和客户名称
            modelMap.put("custNo",cstCustomer.getCustNo());
            modelMap.put("custName",cstCustomer.getCustName());
            modelMap.put("credit",cstCustomer.getCustCredit());

            //根据客户名称查询到对应的订单信息表的记录数,一个客户可以有多个订单
            List<Orders> ordersList = ordersService.selectOrdersByCustName(cstCustomer.getCustName());

            //根据订单日期进行模糊查询 TODO
            if (searchYear != null && !searchYear.equals("")){
                ordersList = ordersService.selectOrdersByCustNameAndDate(cstCustomer.getCustName(),searchYear);
            }

            //用于存放一个用户所有的订单金额的总和
            Double totalMoney = 0.0;
            for (Orders orders : ordersList){
                //再根据订单编号查询到对应的订单明细表,一个订单编号对应一个订单明细表里面的数据
                OrdersLine ordersLine = ordersLineService.selectOrdersLinesByOrderId(orders.getOdrId());
                //一个订单编号对应的一个订单明细表的金额，根据订购的产品数量以及产品单价计算出订单金额
                Double money = ordersLine.getOddPrice()*ordersLine.getOddCount();
                totalMoney += money;
            }
            modelMap.put("total",totalMoney);

            //将一个客户的编号名称，以及订单总额存入外层list
            mapList.add(modelMap);

        }

        return mapList;
    }
}
